/**
 * 微信素材相关管理
 * @authors 杨兴洲（of2502）
 * @date    2017/2/28 15:03
 * @version 1.0
 */
/// <reference path="./weixin-material-service.d.ts" />
import * as rp from "request-promise-native";
import {ReadStream} from "fs";

const HOST = 'https://api.weixin.qq.com/cgi-bin';

export default class WeixinMaterialService {

    /**
     * 获取素材总数
     * @param access_token
     * @returns {Promise}
     */

    static getCount(access_token: String): Promise<MaterialCount | WxError> {
        return rp({
            method: 'GET',
            uri: `${HOST}/get_materialcount`,
            qs: {access_token},
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 获取素材列表
     * @param access_token
     * @param type   素材的类型 image|voice|video|news
     * @param offset 该偏移位置 >=0
     * @param count  返回素材的数量 1~20
     * @returns {Promise}
     */
    static getList({access_token, type, offset, count}: MaterialListOption): Promise<MaterialList | WxError> {
        return rp({
            method: 'POST',
            uri: `${HOST}/material/batchget_material`,
            qs: {access_token},
            body: {type, offset, count},
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /*新增永久图文素材*/
    // 验证条件 文章数<=8
    static addArticle({access_token, articles}: { access_token: string, articles: Article[] }): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/material/add_news`,
            qs: {access_token},
            body: {articles},
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 新增文章内图片
     * @param access_token
     * @param media
     * @returns {Promise}
     */
    static addArticleImg({access_token, media}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/media/uploadimg`,
            qs: {access_token},
            formData: {media},
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 上传临时素材
     * @param access_token
     * @param type
     * @param media
     * @returns {Promise<any>}
     */
    static addTempItem({access_token, type, media}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/media/upload`,
            qs: {access_token},
            formData: {type, media},
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /*修改永久图文素材*/
    static updateArticle({access_token, media_id, index = 0, articles}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/material/update_news`,
            qs: {access_token},
            body: {media_id, index, articles},
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /* 新增永久其他素材
     * 文件form上传等待验证
     * 分别有图片（image）、语音（voice）、视频（video）和缩略图（thumb）
     * @param access_token
     * @param type
     * @param media
     * @param title
     * @param introduction
     * @returns {Promise<T>}
     */
    static addItem({access_token, type, media, title, introduction}: Material): Promise<any> {
        let formData: { type: string, media: ReadStream, description?: string } = {type, media};
        if (type === 'video') formData.description = JSON.stringify({title, introduction});
        return rp({
            method: 'POST',
            uri: `${HOST}/material/add_material`,
            qs: {access_token},
            formData,
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }


    /*获取永久素材*/
    static getItem({access_token, media_id}: { access_token: string, media_id: string }): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/material/get_material`,
            qs: {access_token},
            body: {media_id},
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /*删除永久素材*/
    static deleteItem({access_token, media_id}: { access_token: string, media_id: string }): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/material/del_material`,
            qs: {access_token},
            body: {media_id},
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }
}
